package example;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

public class Exec {
	public static void main(String[] args) {

		SessionFactory sf = HibernateUtil.getSessionFactory(true);
		Session session = null;
		try {
			session = sf.openSession();
			session.getTransaction().begin();
			
			Person person = new Person();
			person.setName("John");
			
						
			session.save(person);		
			session.getTransaction().commit();
			
			session.clear();
			
			session.beginTransaction();
			List<Person> persons = session.createQuery("select p from Person p").list();
			
			StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
			encryptor.setAlgorithm("PBEWithMD5AndDES");
			encryptor.setPassword("123456");
			encryptor.setKeyObtentionIterations(1000);			
			encryptor.initialize();
			for(Person pers : persons){
				pers.setName(encryptor.encrypt(pers.getName()));
				session.save(pers);				
			}
						
			session.getTransaction().commit();
		} catch (Exception ex) {
			try {
				ex.printStackTrace();
				session.getTransaction().rollback();
			} catch (Exception ex2) {
				ex2.printStackTrace();
			}
		} finally {
			session.close();
			HibernateUtil.shutdown();
		}

	}
}
